package cn.zucc.edu.handyzucc.controller;

import cn.zucc.edu.handyzucc.entity.Notice;
import cn.zucc.edu.handyzucc.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/notice")
public class NoticeController {
    @Autowired
    private NoticeService noticeService;

    @RequestMapping(value = "getNotices", method = RequestMethod.GET)
    private Map<String, Object> getNotices() {
        Map<String, Object> modelMap = new HashMap<>();
        List<Notice> notices = noticeService.getNotices();
        modelMap.put("code", "0");
        modelMap.put("info", "查询成功");
        modelMap.put("data", notices);
        return modelMap;
    }

    @RequestMapping(value = "getNoticesByKeyword", method = RequestMethod.POST)
    private Map<String, Object> getNoticesByKeyword(String keyword) {
        Map<String, Object> modelMap = new HashMap<>();
        List<Notice> notices = noticeService.getNoticesByKeyword(keyword);
        modelMap.put("code", "0");
        modelMap.put("info", "查询成功");
        modelMap.put("data", notices);
        return modelMap;
    }

    @RequestMapping(value = "deleteNotice", method = RequestMethod.POST)
    private Map<String, Object> deleteNotice(@RequestBody Map<String, Object> map) {
        Map<String, Object> modelMap = new HashMap<>();
        if (noticeService.deleteNotice(Integer.parseInt(map.get("notice_id").toString()))) {
            modelMap.put("code", "0");
        } else modelMap.put("code", "-1");
        return modelMap;
    }

    @RequestMapping(value = "addNotice", method = RequestMethod.POST)
    private Map<String, Object> addNotice(@RequestBody Map<String, Object> map) throws ParseException {
        Map<String, Object> modelMap = new HashMap<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(sdf.parse(map.get("notice_time").toString()));
        Notice notice = new Notice(map.get("notice_department").toString(), map.get("notice_title").toString(), map.get("notice_content").toString(), map.get("notice_content_md").toString(), sdf.parse(map.get("notice_time").toString()));
        if (noticeService.addNotice(notice))
            modelMap.put("code", "0");
        else modelMap.put("code", "-1");
        return modelMap;

    }

    @RequestMapping(value = "updateNotice", method = RequestMethod.POST)
    private Map<String, Object> updateNotice(@RequestBody Map<String, Object> map) {
        Map<String, Object> modelMap = new HashMap<>();
        Notice notice = new Notice(Integer.parseInt(map.get("notice_id").toString()), map.get("notice_department").toString(), map.get("notice_title").toString(), map.get("notice_content").toString(), map.get("notice_content_md").toString());
        if (noticeService.updateNotice(notice))
            modelMap.put("code", "0");
        else modelMap.put("code", "-1");
        return modelMap;
    }
}
